var slideshow = {};

slideshow.is_ipad = !! window.navigator.userAgent.match ( /ipad/i );

slideshow.fx = {
	"fade-start"  : { "opacity" : "0" },
	"fade-end"    : { "opacity" : "1" },
	"scale-start" : { "transform" : "scale(0,0)" },
	"scale-end"   : { "transform" : "scale(1,1)" },
	"top-start"   : { "transform" : "translate3d(0,-500%,0)" },
	"top-end"     : { "transform" : "translate3d(0,0,0)" },
	"bottom-start"   : { "transform" : "translate3d(0,500%,0)" },
	"bottom-end"     : { "transform" : "translate3d(0,0,0)" },
	"left-start"   : { "transform" : "translate3d(-500%,0,0)" },
	"left-end"     : { "transform" : "translate3d(0,0,0)" },
	"right-start"   : { "transform" : "translate3d(500%,0,0)" },
	"right-end"     : { "transform" : "translate3d(0,0,0)" },
	"rotate-start" : { "transform" : "rotate3d(0,0,1,-360deg)" },
	"rotate-end" : { "transform" : "rotate3d(0,0,1,0)" },
	"flip-start" : { "transform" : "rotate3d(0,1,0,-180deg)" },
	"flip-end" : { "transform" : "rotate3d(0,1,0,0)" }
};

slideshow.init = function ( base_elem, data )
{
	document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);

	var t, l = data.length;
	var tot_pages = 0;
	

	// data.push ( { "title" : "(end)", "start-page" : _images.length } );

	slideshow._elements = [];
	slideshow._timeouts = [];

	for ( t = 0; t < l; t ++ )
	{
		tot_pages += data [ t ].slides.length; //slideshow._create_section ( base_elem, t, data [ t ], start_page );
	}

	slideshow._create_gallery ( base_elem, tot_pages );
	
	for ( t = 0; t < l; t ++ )
	{
		slideshow._create_section ( data [ t ] );
	}

	slideshow._init ();
};

slideshow._create_gallery = function ( base_elem, tot_pages )
{
	slideshow._create_elem ( base_elem );
	slideshow._gallery = new SwipeView ( '#wrapper', { numberOfPages: tot_pages }, "gal" );
	slideshow._last_page = 0;
	slideshow._slides = [];
	slideshow._objects = [];
	slideshow._sections = [];
};

slideshow._create_section = function ( section )
{
	var	gallery = slideshow._gallery, i, t;

	slideshow._add_section ( section );

	for ( t = 0; t < section.slides.length; t ++ )
	{
		i = _images [ slideshow._last_page ++ ];

		if ( slideshow.is_ipad ) i.img = i.img.replace ( "jpg", "pdf" );

		slideshow._slides.push ( 
			{ 
				"img" : "images/" + i.img,
				"width" : 1000, //i.width,
				"height" : 700, // i.height,
				"desc" : t
			}
		);

		slideshow._objects.push ( section.slides [ t ] );
	}
};

slideshow._add_section = function ( section )
{
	slideshow._sections.push ( { start_page : slideshow._last_page, section: section } );

	var btn = document.createElement ( "button" );
	
	btn.innerHTML = section.title; /* slideshow._sections.length; */
	btn.onclick = function ( page ) { return function () { slideshow._gallery.goToPage ( page ); } } ( slideshow._last_page );

	document.getElementById ( "btn-panel" ).appendChild ( btn );
};


slideshow._init = function ()
{
	var gallery = slideshow._gallery, i, page, el, slides = slideshow._slides;

	// Load initial data
	for (i=0; i<3; i++) 
	{
		page = i==0 ? slides.length-1 : i-1;
		el = document.createElement('img');
		el.className = 'loading';
		el.src = slides[page].img;
		el.width = slides[page].width;
		el.height = slides[page].height;
		el.onload = function () { this.className = ''; }
		gallery.masterPages[i].appendChild(el);
	}

	gallery.onFlip(function () 
	{
		var el, upcoming, i;

		for ( i=0; i < 3; i++ ) 
		{
			upcoming = gallery.masterPages[i].dataset.upcomingPageIndex;

			if ( upcoming != gallery.masterPages[i].dataset.pageIndex) {
				el = gallery.masterPages[i].querySelector('img');
				el.className = 'loading';
				el.src = slides[upcoming].img;
				el.width = slides[upcoming].width;
				el.height = slides[upcoming].height;
			}
		}

		slideshow._objects_create ( gallery.page );
	});


/*
	gallery.onMoveOut(function () 
	{
		gallery.masterPages[gallery.currentMasterPage].className = gallery.masterPages[gallery.currentMasterPage].className.replace(/(^|\s)swipeview-active(\s|$)/, '');
	});

	gallery.onMoveIn(function () 
	{
		//var className = gallery.masterPages[gallery.currentMasterPage].className;
		// /(^|\s)swipeview-active(\s|$)/.test(className) || (gallery.masterPages[gallery.currentMasterPage].className = !className ? 'swipeview-active' : className + ' swipeview-active');
	});

	gallery.updatePageCount ( function ()
	{
		debugger;
	} );
*/
};

slideshow._create_elem = function ( base_el )
{
	var el = document.createElement ( "div" );
	var pnl;

	el.className = "wrapper";
	el.id = "wrapper";
	base_el.appendChild ( el );

	el = document.createElement ( "div" );
	el.className = "navbar";
	el.id = "navbar";
	base_el.appendChild ( el );

	pnl = document.createElement ( "div" );
	pnl.id = "btn-panel";
	el.appendChild ( pnl );

	pnl = document.createElement ( "div" );
	pnl.id = "nav-toggler";
	pnl.onclick = function ()
	{
		var e = document.getElementById ( "navbar" );
		
		if ( e.className.indexOf ( "open" ) != -1 )
			e.className = "navbar";
		else
			e.className = "navbar open";
	};

	el.appendChild ( pnl );
};

slideshow._objects_create = function ( page_num )
{
	var gallery = slideshow._gallery;
	var objects = slideshow._objects [ page_num ];

	var actions = {
		"video" : slideshow._o_video,
		"image" : slideshow._o_image,
		"html"  : slideshow._o_html,
		"audio" : slideshow._o_audio
	}, f;

	slideshow._el_clear ();

	if ( ! objects ) return;

	var t, l = objects.length, o;

	for ( t = 0; t < l; t ++ )
	{
		o = objects [ t ];

		if ( ! o.timeout ) o.timeout = 0;

		if ( ! o.type ) 
			console.error ( "Missing 'type' for: %o", o );
		else {
			f = actions [ o.type ];
			if ( ! f ) 
			{
				console.error ( "Unsupported type: %s for %o", o.type, o );
			} else 
				slideshow._timeout_cback ( f, gallery, o );
		}
	}
};

slideshow._timeout_cback = function ( f, gallery, o ) 
{ 
	slideshow._timeouts.push ( setTimeout ( function () { 
		var el = f ( gallery, o ); 

		if ( o.border ) el.className = 'border';

		if ( o.fx )
		{
			var effects = o.fx.split ( " " ), i, e, fx;
			var start = { "opacity" : [], "transform" : [] },
			    end   = { "opacity" : [], "transform" : [] };

			for ( i = 0; i < effects.length; i ++ )
			{
				e = effects [ i ];
				slideshow._fx_add ( e, "start", start );
				slideshow._fx_add ( e, "end", end );
			}
			
			//el.style.WebkitTransition = "opacity 1s, -webkit-transform 1s";
			el.style [ '-webkit-transition' ] = "opacity 1s, -webkit-transform 1s";

			slideshow._set_styles ( el, start );
			setTimeout ( function () { slideshow._set_styles ( el, end ); }, 50 );
		}

	}, o.timeout * 1000 ) );
};

slideshow._fx_add = function ( e, mode, data )
{
	var fx = slideshow.fx [ e + "-" + mode ];

	if ( fx.opacity )   data.opacity.push   ( fx.opacity );
	if ( fx.transform ) data.transform.push ( fx.transform );
};

slideshow._set_styles = function ( el, data )
{
	if ( data.opacity )    el.style.opacity = data.opacity.join ( " " );
	if ( data.transform )  el.style [ '-webkit-transform' ] = data.transform.join ( " " );
};


slideshow._o_video = function ( gallery, o )
{
	var el = slideshow._el_create ( gallery, o );

	el.innerHTML = '<video src="' + o.src + '" type="video/mp4" width="100%" height="100%" controls="controls" autoplay></video>';

	return el;
};

slideshow._o_image = function ( gallery, o )
{
	var el = slideshow._el_create ( gallery, o );

	el.innerHTML = '<img src="' + o.src + '" width="100%" height="100%" />';

	return el;
};

slideshow._o_html = function ( gallery, o )
{
	var el = slideshow._el_create ( gallery, o );

	el.innerHTML = o.text;

	return el;
};

slideshow._o_audio = function ( gallery, o )
{
	var el = slideshow._el_create ( gallery, o );

	el.style.backgroundColor = "red";
	el.innerHTML = '<audio autoplay="1"><source src="' + o.src + '" type="audio/mp3"/></audio>';

	return el;
};

slideshow._el_create = function ( gallery, o )
{
	var el = document.createElement ( 'div' );
	var root = slideshow._el_get_masterpage ( gallery );

	el.style.width  = o.w + "px";
	el.style.height = o.h + "px";
	el.style.left   = o.x + "px";
	el.style.top    = o.y + "px";
	el.style.position = 'absolute';

	slideshow._elements.push ( el );

	root.appendChild ( el );
	return el;
};

slideshow._el_get_masterpage = function ( gallery )
{
	return document.querySelector ( "#slides .swipeview-active" );
};

slideshow._el_clear = function ()
{
	var t, l = slideshow._elements.length;
	var e;

	for ( t = 0; t < l; t ++ )
	{
		e = slideshow._elements [ t ];

		e.parentNode.removeChild ( e );
	}
	
	slideshow._elements = [];

	l = slideshow._timeouts.length;
	for ( t = 0; t < l; t ++ )
		clearTimeout ( slideshow._timeouts [ t ] );

	slideshow._timeouts = [];
};

// Slideshow _actions definition
